package com.bac.rentmap.web.controller.cron;

import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.ad.batch.correction.CorrectionCalculator;

public class RandomCorrectionController extends AbstractController {
    private static final Logger log = Logger
            .getLogger(RandomCorrectionController.class.getName());
    private Random random = new Random();
    private List<String> correctionUrls;
    private CorrectionCalculator correctionCalculator;
    private RandomCronController randomCronController;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        int i = random.nextInt(correctionUrls.size());
        String correctionUrl = correctionUrls.get(i);
        log.info("Checking correction for " + correctionUrl);
        if(correctionCalculator.updateCorrection(correctionUrl)){
            randomCronController.resetCorrectionCache();
        }
        return null;
    }

    @Required
    public void setCorrectionUrls(List<String> correctionUrls) {
        this.correctionUrls = correctionUrls;
    }

    public void setCorrectionCalculator(CorrectionCalculator correctionCalculator) {
        this.correctionCalculator = correctionCalculator;
    }

    public void setRandomCronController(RandomCronController randomCronController) {
        this.randomCronController = randomCronController;
    }
}
